<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8" isELIgnored="false"%>
<%@ include file="/WEB-INF/views/shared/taglib.jsp"%>
<%
	request.setAttribute("modelConfigPath", "cargo");
%>
<%@ include file="/WEB-INF/views/shared/master/contentHeader.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/index.jsp"%>
<style type="text/css" >
	body{
		padding: 0px 0px !important;
		overflow: hidden;	
	}
	
	.panel{
		margin: 0px 0px !important;
	}
	
	.pull-right-important{
		float:right !important;
	}
</style>
<t:form modelAttribute="collocationSearch" method="post" action="/shop/supplyCargo/collocation/index" ajaxPost="true" onSuccess="onSuccess">
	
	<t:panel cssClass="panel-flat" customBody="true" customHead="true">
		<t:hidden path="supplierId"/>		
		
		<div class="panel-body">
			<div class="row">
				<c:import url="input.jsp" />
			</div>
			<div id="tableContent" class="row" style="overflow:auto;">
				<c:import url="output.jsp" />
			</div>
		</div>
	</t:panel>
	<t:panelFoot headingBtnCssClass="heading-btn pull-right-important">
		<t:button icon="icon-floppy-disk" onclick="closed()" laddaType="progress" cssClass="btn-info" csize="sm">保存</t:button>
	</t:panelFoot>
</t:form>

<script type="text/javascript">
	var collocationIds = [];
	
	$(function(){
		//重新进入时保持选中	
		bindClick();
		//监听点击事件后将点击对象保存进数组
		
		trClickListener();
		
		resizeEx();
		$(window).resizeend({delay : 500}, function() {
			resizeEx();
		});
	});

	function resizeEx(){
		var parentIframe=window.name;
		console.log(parentIframe);
	 if(parentIframe=="supplyCargo_form"){
			$("#tableContent").height($(window).height()-117);
			$(".panel-body").height($(window).height()-95)
		}
		
	}
	function onSuccess(res){
		$("#tableContent").html(res);
		bindClick();
		trClickListener();
		
		redrawTable();
	}
	function searchCallback(){
		formSubmit();
	}
	function pagerChangeCallback(){
		formSubmit();
	}
	function sortingCallback(){
		formSubmit();
	}
	function formSubmit(){
		$("form:first").submit();	
	}
	
	function closed(){	
		var winId=$.getUrlParam("windowId");
		var parentWindowId = $.getUrlParam("windowId").top;
		
		if(collocationIds.length != 0){
			$.ajax({
		        type: 'post',
		        dataType: "json",
		        url: $.getVirtualPath()+"/shop/supplyCargo/save",
		        data: "supplierId="+$("#supplierId").val()+"&collocationIds="+collocationIds,
		        async: false,
		        success: function (result) {
		       		if("${t_type}" =="addPurchaseCollocation"){
		       			console.log("${t_type}" );
		 				top.purchase_form.assetModalClose();
		 				closeModal();
		 			}else{
		 				top.main.supplyCargoIframe.formSubmit();
			    		$.topNotify(result.message,result.resultType);
						closeModal();	
		 			}
		    		
		        }
		    });
		}else{
			$.topNotify("未保存任何数据","warning");
			top.$("#modal_"+winId).modal("hide");
		}
		
	} 

	function bindClick(){
		
		 for(var i=0;i<collocationIds.length;i++){
			 $("tr[data-id="+collocationIds[i]+"]").addClass("select-tr");
			 
	     }
	} 

	function trClickListener(){
		
		$("#tableContent table tbody tr").click(function(){
			var selectedId = $(this).data("id");
			
			var position = -1;
			for(var i=0;i<collocationIds.length;i++){
				if(collocationIds[i] == selectedId){
					position = i;					
				}
		     }
			if(position>-1){
				collocationIds.splice(position,1);
			}else{
				collocationIds.push(selectedId);
			}		
		});
	}

	
</script>

<%@ include file="/WEB-INF/views/shared/master/cntable.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/jqueryui.jsp"%>
<%@ include file="/WEB-INF/views/shared/master/dialog.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/notifications.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formbuttons.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formstyling.jsp"%>
<%@ include file="/WEB-INF/views/shared/base/scripts/limitless/default/formselects.jsp"%>
<%@ include file="/WEB-INF/views/shared//master/contentFooter.jsp"%>